(function (window) {
    function loadJSBridge(main) {
        class TJSBridge {
            constructor() {
                const self = this;
                return function JSBFn(opts, cb) {
                    if (self[this]) {
                        self[this](opts, cb);
                    }
                }
            }

            getCurrentLocation(opts, cb) {
                var args = getDefaultParameter('getCurrentLocation', cb);
                window.JSBridge.call('getCurrentLocation', JSON.stringify(args))
            }

            startContinuousLocation(opts, cb) {
                var args = getParameter('startContinuousLocation', JSON.stringify(opts), cb);
                window.JSBridge.call('startContinuousLocation', JSON.stringify(args))
            }

            stopContinuousLocation(cb) {
                var args = getParameter('stopContinuousLocation', cb);
                window.JSBridge.call('stopContinuousLocation', JSON.stringify(args))
            }

            getSystemInfo(cb) {
                var args = getDefaultParameter('getSystemInfo', cb);
                window.JSBridge.call('getSystemInfo', JSON.stringify(args))
            }

            getDeviceId(cb) {
                var args = getDefaultParameter('getDeviceId', cb);
                window.JSBridge.call('getDeviceId', JSON.stringify(args))
            }

            setTitle(opts) {
                var args = getParameter('setTitle', opts, '');
                window.JSBridge.call('setTitle', JSON.stringify(args))
            }

            setClipboard(param) {
                var args = getParameter('setClipboard', param, '');
                window.JSBridge.call('setClipboard', JSON.stringify(args))
            }

            pushWindow(opts) {
                var args = getParameter('pushWindow', opts, '');
                window.JSBridge.call('pushWindow', JSON.stringify(args))
            }

            popWindow() {
                window.JSBridge.call('popWindow', '{}')
            }

            setGestureBack(opts) {
                var args = getParameter('setGestureBack', opts, '');
                window.JSBridge.call('setGestureBack', JSON.stringify(args))
            }

            hideTopBar() {
                window.JSBridge.call('hideTopBar', '{}')
            }

            tradePay(opts, cb) {
                var args = getDefaultParameter('tradePay', cb);
                window.JSBridge.call('tradePay', JSON.stringify(args))
            }

            getAuthData(cb) {
                var args = getDefaultParameter('getAuthData', cb);
                window.JSBridge.call('getAuthData', JSON.stringify(args))
            }

        }

        window.JSBridge = new TJSBridge();
        window.JSBridgeKeys = Object.getOwnPropertyNames(TJSBridge.prototype);
        main();
    }

    function getParameter(funcName, opts, cb) {
        return getArgs(funcName, opts, cb);
    }

    function getDefaultParameter(funcName, cb) {
        return getArgs(funcName, "", cb);
    }

    function getArgs(funcName, opts, cb) {
        var args = {
            parameter: opts,
        };
        if (cb && (typeof cb === 'function')) {
            var cbName = funcName;
            window[cbName] = cb;
            args['cbName'] = cbName;
        }
        return args;
    }

    window.loadJSBridge = loadJSBridge;
    loadJSBridge(() => {
        console.log('loadJSBridge success');
    });
})(window);


